package com.xiaoq.matrix.service.impl;

import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaoq.AbstractReceiver;
import com.xiaoq.coms.util.JsonUtil;
import com.xiaoq.matrix.service.IContentService;
import com.xiaoq.matrix.vo.PublishTaskVo;

@Component("contentPublishReceiver")
public class ContentPublishReceiver extends AbstractReceiver {
	private static final Logger LOG = LoggerFactory.getLogger(ContentPublishReceiver.class);
	private static final String LOG_PREFIX = "[MATRIX] [SMS] [QUEUE] ";
	
	@Autowired
	private IContentService contentService;
	
	@Override
	public void execute(String message) {
		LOG.info(MessageFormat.format(LOG_PREFIX + "收到消息, {0}", message));
		PublishTaskVo task = null;
		try {
			task = JsonUtil.asObject(message, PublishTaskVo.class);
		} catch (Exception e) {
			String msg = LOG_PREFIX + "消息解析错误";
			LOG.info(msg);
//			LOG.error(msg, e);
//			throw new RuntimeException(msg, e);
		}

		LOG.info(LOG_PREFIX + "消息解析完毕");
		
		try {
			contentService.doPublish(task);
		} catch (Exception e) {
			LOG.info("mq消息解析出错");
		}
		
		LOG.info(LOG_PREFIX + "接收消息处理完毕");
	}
}